<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      autocomplete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">德宏师范高等专科学校招聘管理系统</h3>
        <!-- <h6 >建议使用Chrome浏览器，不要使用IE浏览器</h6> -->
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>
      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <el-row>
      <el-form-item prop="yzcode" >
        <span class="svg-container">
          <svg-icon icon-class="eye-open" />
        </span>
        <el-input 
          ref="yzcode"
          v-model="yzcode"
          placeholder="验证码"
          name="yzcode"
          type="text"
          tabindex="3"
          autocomplete="on"
          style="width:70%;"         
        />
        <span style="display: inline-block;width:30px;color:#fff;font-size:16px;" @click="createCode">{{checkCode}}</span>
      </el-form-item>
      </el-row>
      
      <el-row justify="center">

      <el-button
        :loading="loading"
        type="primary"
        style="width:45%;float:left"
        @click.native.prevent="handleLogin"
      >登录
      </el-button>
<el-button
        :loading="loading"
        type="success"
        style="width:45%;float:right"
        @click.native.prevent="handleRegist"
      >注册
      </el-button> 

      </el-row>
      <div class="tips"  style="float:right;margin-top:20px">              
        <a v-on:click="retrievePWD">忘记密码?</a>
      </div>
    </el-form>
      <el-row type="flex" style="margin-top:100px;" justify="center">
        <el-col :xs="18" :sm="14" style="color:#fff;font-size:16px;line-height:25px;text-align:center;vertical-align: middle;">
          <a href="http://bszs.conac.cn/sitename?method=show&amp;id=337D9831D5026B22E053022819AC8498" target="_blank"><img border="0" hspace="0" src="https://dhszzp.top/image/2yjdpawpix.png" width="28" height="30" style="height: 30px;width: 28px">
          版权所有：德宏师范高等专科学校人事处 电话：0692-3028397</a>
          <br>
          <a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=53310302000028">
          <img border="0" hspace="0" src="https://dhszzp.top/image/2yqyt17pj4.png" width="28" align="center" height="30" style="height: 30px;width: 28px">
        </a>&nbsp滇公网安备 53310302000028  滇ICP备17006966号-3
        </el-col>
      </el-row>  
    
  </div>
    
</template>

<script>
import Cookies from 'js-cookie'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value || value.length === 0) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (!value || value.length === 0) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'change', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
        //yzcode: [{ required: true, message: "请输验证码", trigger: "blur" }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      checkCode: '',
      yzcode: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
    this.createCode()
  },
  methods: {
    createCode() {
      let code = "";
      const codeLength = 4; //验证码的长度  
      const random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
      'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数  
      for(let i = 0; i < codeLength; i++) { //循环操作  
        let index = Math.floor(Math.random() * 36); //取得随机数的索引（0~35）  
        code += random[index]; //根据索引取得随机数加到code上  
      }
      this.checkCode = code; //把code值赋给验证码  
      //console.log(this.checkCode)
    },
    checkCapslock({ shiftKey, key } = {}) {
      //console.log(shiftKey, key)
      if (key && key.length === 1) {
        if ((shiftKey && (key >= 'A' && key <= 'Z')) || (!shiftKey && (key >= 'A' && key <= 'Z'))) {
          this.capsTooltip = true
        } else {
          this.capsTooltip = false
        }
      } else {
        this.capsTooltip = false
      }
      if (key === 'CapsLock' && this.capsTooltip === true) {
        this.capsTooltip = false
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if(this.yzcode==""){
        this.$message({ message:"验证码不能为空", type: 'error' })
        return false;
      }else if(this.yzcode != this.checkCode){
          this.$message({ message:"验证码错误，请注意大小写", type: 'error' })
          this.createCode();
          return false;
      }else{
          this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            //console.log(this.loginForm.role)
            Cookies.set('username',this.loginForm.username)
            this.$store.dispatch('user/login', this.loginForm)
              .then(() => {
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
                this.loading = false
              })
              .catch(() => {
                this.loading = false
              })
          } else {
            //console.log('error submit!!')
            return false
          }
        })
      }
      
    },
    handleRegist(){
      this.$router.push({ path: '/regist' })
    },
    retrievePWD(){
      this.$router.push({ path: '/retrievePWD' })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg: #383443;
  $light_gray: #fff;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;

    .login-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }
</style>
